package com.better.frontend.controller.paper;

import com.better.backend.mqReceiverService.calType.model.CalTypeModel;
import com.better.backend.mqReceiverService.calType.repository.CalTypeRepsitory;
import com.better.backend.mqReceiverService.questionReceiver.model.ExamPaperModel;
import com.better.backend.mqReceiverService.questionReceiver.model.PointPaperModel;
import com.better.common.base.ServiceResponse;
import com.better.common.utils.CmsHttpPostUtil;
import com.better.common.utils.GlobalUtil;
import com.better.common.utils.ServiceResponseHandle;
import com.better.frontend.Iservice.paper.PaperService;
import com.better.frontend.model.paper.PaperVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.apache.juli.logging.Log;
import org.apache.juli.logging.LogFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;


/**
 * 编辑试卷
 * Created by xq on 2017/11/19.
 */
@Api(value = "编辑试卷", tags = "编辑试卷")
@RestController
@RequestMapping("/api/paper")
public class PaperController {
    private static Log log = LogFactory.getLog(PaperController.class);
    @Resource
    private PaperService paperService;
    @Value("${pointUrl}")
    private String pointUrl;

    /**
     * 导入试卷
     * @param excelFile
     * @param paperModel
     * @return
     * @throws Exception
     */
    @ApiOperation(value = "导入试卷", notes = "导入试卷")
    @RequestMapping(value = "/importPaper", method = RequestMethod.POST)
    @ResponseBody
    public ServiceResponse<?> importPaper(MultipartFile excelFile,String examId, String subId, String gradeId)  throws Exception{
        if (log.isDebugEnabled()) {
            log.debug("调用接口:[importPaper],参数:[importPaper =]" +examId+"。subId:"+subId+"。gradeId:"+gradeId);
        }
        ExamPaperModel paperModel = new ExamPaperModel();
        paperModel.setExamid(examId);
        paperModel.setSubjectid(subId);
        paperModel.setGradeid(gradeId);
        return paperService.savePaper(paperModel,excelFile);
    }

    /**
     * 试卷查询
     * @param
     * @param
     * @return
     * @throws Exception
     */
    @ApiOperation(value = "试卷查询", notes = "试卷查询")
    @RequestMapping(value = "/selectPaper", method = RequestMethod.GET)
    @ResponseBody
    public ServiceResponse<?> selectPaper(String examId, String subId, String gradeId){
        if (log.isDebugEnabled()) {
            log.debug("调用接口:[importPaper],参数:[importPaper =]" +examId+"。subId:"+subId+"。gradeId:"+gradeId);
        }
        ExamPaperModel paperModel = new ExamPaperModel();
        paperModel.setExamid(examId);
        paperModel.setSubjectid(subId);
        paperModel.setGradeid(gradeId);
        return paperService.selectPaper(paperModel);
    }

    /**
     * 保存知识点
     * @param
     * @param
     * @param
     * @param
     * @param
     * @return
     */
    @ApiOperation(value = "编辑知识点", notes = "编辑知识点")
    @RequestMapping(value = "/editPoint", method = RequestMethod.POST)
    @ResponseBody
    public ServiceResponse<?> editPoint(@RequestBody PaperVo paperVo){
        if (log.isDebugEnabled()) {
            log.debug("调用接口:[editPoint],参数:[editPoint =]" + paperVo.getQuesId() + "。examId:" + paperVo.getExamID() + "。subId:" + paperVo.getSubId() + "。gradeId:" + paperVo.getGradeId() + "。知识点集合长度：" + paperVo.getPointPaperModels().size());
        }
        return paperService.savePoint(paperVo.getPointPaperModels(),paperVo.getQuesId(),paperVo.getExamID(),paperVo.getSubId(),paperVo.getGradeId());
    }

    @ApiOperation(value = "添加分析模板", notes = "添加分析模板")
    @RequestMapping(value = "/addCalType", method = RequestMethod.POST)
    @ResponseBody
    public ServiceResponse<?> addCalType(@RequestBody CalTypeModel calTypeModel){
        if (log.isDebugEnabled()) {
            log.debug("调用接口:[addCalType],参数:[addCalType =]" + calTypeModel.toString());
        }
        if(StringUtils.isEmpty(calTypeModel.getId())){
            calTypeModel.preInsert();
        }
        else{
            calTypeModel.preUpdate();
        }
        return paperService.saveCalType(calTypeModel);
    }

    /**
     * 发布试卷
     * @param examID
     * @return
     */
    @ApiOperation(value = "发布试卷", notes = "发布试卷")
    @RequestMapping(value = "/publishPaper", method = RequestMethod.GET)
    @ResponseBody
    public ServiceResponse<?> publishPaper(String examID){
        if (log.isDebugEnabled()) {
            log.debug("调用接口:[publishPaper],参数:[publishPaper =]" + examID);
        }
        return paperService.publishPaper(examID);
    }

    /**
     * 获取科目知识点
     * @param gradeId
     * @param subjectId
     * @return
     */
    @ApiOperation(value = "获取科目知识点", notes = "获取科目知识点")
    @RequestMapping(value = "/getPoints", method = RequestMethod.GET)
    @ResponseBody
    public ServiceResponse<?> getPoints(String gradeId, String subjectId){
        if (log.isDebugEnabled()) {
            log.debug("调用接口:[getPoints],参数:[getPoints =]年级ID：" + gradeId+"。科目ID："+subjectId);
        }
        return CmsHttpPostUtil.HttpsGet(pointUrl + "gradeId="+gradeId+"&subjectId="+subjectId, new HashMap<>(16));
    }
}
